using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace Framework
{
    public static class UITextShow
    {
        public static void PlayTextOneByOne(this Text text, string content, float interval,Action cb)
        {
            text.StartCoroutine(ShowText(text, content, interval,cb));
        }

        static IEnumerator ShowText(Text text, string content, float interval,Action cb)
        {
            int i = 0;
            string s = null;
            while (i < content.Length)
            {
                yield return new WaitForSecondsRealtime(interval);
                s += content[i].ToString();
                text.text = s;
                i++;
            }
            cb();
        }

        public static void PlayTextFadeIn(this Text text, string content,float lineInterval, Action cb)
        {
            text.StartCoroutine(ShowTextFadeIn(text, content, lineInterval, cb));
        }

        static IEnumerator ShowTextFadeIn(Text text, string content, float lineInterval, Action cb)
        {
            string[] strArr = content.Split(new string[] {"\n"}, StringSplitOptions.None);
            foreach (var str in strArr)
            {
                string textStr = str + "\n";
                text.text += textStr;
                yield return new WaitForSecondsRealtime(lineInterval);
            }

            cb();
        }
    }
}